package cn.net.lwj.common.exception;

public class SjtbException extends Exception {

    protected Integer code;
    protected String message;

    /*public SjtbException(String message) {
        this.message = message;
    }*/

    public SjtbException(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public SjtbException(CodeInfo codeInfo) {
        this.code = codeInfo.getCode();
        this.message = codeInfo.getValue();
    }

    public SjtbException(CodeInfo codeInfo, String... params) {
        this.code = codeInfo.getCode();
        this.message = codeInfo.getValue(params);
    }

    public String getFullMessage() {
        if (code != null) {
            return "【" + code + "】" + message;
        }
        return message;
    }
    public String getMessage() {
        return message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 异常信息
     */
    public enum CodeInfo {
        required(10000, "参数有误,@REPLACE@不能为空!"),
        xmlExc(10001, "xml解析异常,@REPLACE@!"),
        sizeExc(10002, "zip包大于500M,需要分包!"),
        sjjdExc(10003, "数据阶段编码有误!"),
        nameExc(10004, "文件命名异常!"),
        noXmlStrExc(10005, "未解析到xml文件!"),
        jzExc(10006, "实体文件异常:@REPLACE@!"),
        otherExc(10009, "@REPLACE@!"),
        ;
        Integer code;
        String value;

        CodeInfo(Integer code, String value) {
            this.code = code;
            this.value = value;
        }
        public Integer getCode() {
            return code;
        }
        public String getValue() {
            return value;
        }

        public String getValue(String... replaceStr) {
            if (replaceStr != null && !"".equals(replaceStr[0])) {
                return value.replace("@REPLACE@", replaceStr[0]);
            }
            return value;
        }
    }

    public static void main(String[] args) {
        String str = "" ;
        int a = 10;
        try {
            //int s = 10 / 0;
            if ("".equals(str)) {
                //throw new SjtbException(CodeInfo.required.getCode(), CodeInfo.required.getValue("mlid"));
                //throw new SjtbException(CodeInfo.required.getCode(), CodeInfo.required.getValue(""));
                //throw new SjtbException(CodeInfo.nameExc);
                //throw new SjtbException("55555555555555555555555");
            }
        } /*catch (SjtbException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } */catch (Exception e) {
            e.printStackTrace();
        }
    }
}
